import wanlshop_config from '@/common/config/config';
//免登录接口
let noLoginUrl = [
	'/addons/ask/api.common/config',
	'/addons/ask/api.login/login',
	'/addons/ask/api.login/register',
	'/addons/ask/api.login/thirdLogin',
	'/addons/ask/api.login/resetpwd',
	'/addons/ask/api.user/myinfo',
	'/addons/ask/api.user/answer',
	'/addons/ask/api.user/question',
	'/addons/ask/api.user/article',
	'/addons/ask/api.user/follower',
	'/addons/ask/api.user/attention',
	'/addons/ask/user/getSigned',
	'/addons/ask/api.question/index',
	'/addons/ask/api.question/show',
	'/addons/ask/api.article/index',
	'/addons/ask/api.article/show',
	'/addons/ask/api.zone/index',
	'/addons/ask/api.tag/index',
	'/addons/ask/api.tag/show',
	'/addons/ask/api.expert/index',
	'/addons/ask/api.sms/send',
	'/addons/ask/api.ems/send',
	'/addons/ask/api.ajax/get_answer_list',
	'/addons/ask/api.ajax/get_comment_list',
	'/addons/ask/api.ajax/get_question_list',
	'/addons/ask/api.ajax/get_article_list',
	'/addons/ask/api.common/aboutus',
	'/addons/ask/api.common/agreement',
	'/addons/ask/api.login/wxLogin',
	'/addons/ask/api.login/appLogin',
	'/addons/ask/api.login/mobilelogin',
	'/addons/ask/api.login/getWechatMobile',
	'/addons/ask/api.search/index',
	'/addons/recharge/api.recharge/config',
	'/addons/third/api/getAuthUrl',
	'/addons/third/api/callback',
	'/addons/third/api/account',
	'/addons/ask/api.common/captcha',

];
//设置session_id
const getSessionId = function(vm) {
	let session = vm.$util.getDb('session');
	if (!session) {
		let guid = vm.$u.guid();
		vm.$util.setDb('session', guid);
		return guid;
	}
	return session;
}
// 这里的vm，就是我们在vue文件里面的this，所以我们能在这里获取vuex的变量，比如存放在里面的token
// 同时，我们也可以在此使用getApp().globalData，如果你把token放在getApp().globalData的话，也是可以使用的
const install = (Vue, vm) => {
	let url = wanlshop_config.vehicleurl
	// #ifdef H5
	if (typeof window.fastUrl !== 'undefined') {
		url = window.fastUrl;
	}
	// #endif
	Vue.prototype.$u.http.setConfig({
		baseUrl: url,
		header: {
			'content-type': 'application/json'
		},
		originalData: true
	});
	// 请求拦截，配置Token等参数
	Vue.prototype.$u.http.interceptor.request = (config) => {
		// console.log(1111111111,config.header)
		if (config.header.type != 'im') {
			if (noLoginUrl.indexOf(config.url) == -1 && !uni.getStorageSync("token")) {
				// vm.$u.route('/pages/user/auth/name');
				uni.reLaunch({
					url: `/pages/user/auth/name`
				})
				return false;
			}
		}
		//在需要登录的接口，请求前判断token 是否存在,不存在则到登录

		config.header.type = 'ask'
		if (uni.getStorageSync("token")) {
			config.header.token = uni.getStorageSync("token");
		}
		//设置session_id
		config.header.sid = getSessionId(vm);
		config.header.uid = vm.vuex_user && vm.vuex_user.id || 0;
		const res = uni.getDeviceInfo();
		config.header.platform = res.platform || '';
		config.header.model = res.model || '';
		config.header.brand = res.brand || '';
		config.header['x-requested-with'] = 'xmlhttprequest';
		if (config.method == 'POST') {
			config.data['__token__'] = vm.vuex__token__;
		}
		return config;
	}
	// 响应拦截，判断状态码是否通过
	Vue.prototype.$u.http.interceptor.response = (res) => {
		let result = res.data || {};
		//返回__token__,设置
		if (res.header && res.header.__token__) {
			vm.$u.vuex('vuex__token__', res.header.__token__);
			vm.$store.commit('community/setVuexToken', res.header.__token__)
		}
		if (result.data && result.data.__token__) {
			vm.$u.vuex('vuex__token__', result.data.__token__);
			vm.$store.commit('community/setVuexToken', result.data.__token__)
		}
		switch (result.code) {
			case 1:
			case 0:
				return res.res;
			case 401:
				//需要登录的接口，当token 过期时，清掉本地token，到登录页面
				vm.$u.vuex('vuex_token', '');
				// vm.$u.route('/pages/user/auth/name');
				uni.reLaunch({
					url: `/pages/user/auth/name`
				})
				return res.res;
			case 403: //没有权限访问
			case 500: //没有权限访问
				uni.showToast({
					icon: "none",
					title: result.msg
				})
				return res.res;
			default:
				if (res.statusCode == 200) {
					return res.res;
				} else {
					vm.$u.toast('网络请求错误！');
					return false;
				}
		}
	}
}

export default {
	install
}